package cn.tedu.boyou.mgrsite.controller;

import cn.tedu.boyou.mgrsite.util.UploadUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("")
public class UploadController {

    @Value("${file.path}")
    private String filePath;
    @RequestMapping("/uploadImg")
    @ResponseBody
    public String uploadImg(MultipartFile pic){
        //filePath
        return UploadUtil.upload(pic,filePath);
    }

    @RequestMapping("/uploadImg_ck")
    @ResponseBody
    public Object upload(MultipartFile upload){
        Map<String, Object> map =  new HashMap<>();
        String imagePath = null;
        if(upload != null && upload.getSize() > 0){
            try {
                imagePath = UploadUtil.upload(upload,filePath);
                map.put("uploaded",1);
                map.put("url","/"+imagePath);
            }catch (Exception e){
                e.printStackTrace();
                map.put("uploaded",0);
                Map<String, Object> mm =  new HashMap<>();
                mm.put("message",e.getMessage());
                map.put("error",mm);
            }
        }
        return map;
    }

}
